//
//  HWGameCommunityAllListSheet.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/10/24.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import JXSegmentedView
import IQKeyboardManagerSwift
class HWGameCommunityAllListSheet: MMPopupView {
    private var tapBgView:UIView!
    private var bgView: UIView!
    var titleLabel:UILabel!
    var closeButton:UIButton!
    
    ///标签
    var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    var segmentedDataSource: JXSegmentedTitleDataSource!
    var listContainerView: JXSegmentedListContainerView!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWGameCommunityAllListSheet {
    private func initUI() -> Void {
        
        IQKeyboardManager.shared.shouldResignOnTouchOutside = true
        IQKeyboardManager.shared.enableAutoToolbar = false
        
        type = MMPopupType.sheet
        backgroundColor = UIColor.colorWithAlphaHexString(hexString: "#272727", alpha: 0.85)
        attachedView = UIApplication.shared.windows.filter({ $0.isKeyWindow }).last?.rootViewController?.view
        
        tapBgView = UIView.init()
        tapBgView.backgroundColor = UIColor.clear
        addSubview(tapBgView)
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(bacgroundTap(_:)))
        tapBgView.addGestureRecognizer(tap)
        
        bgView = UIView()
        bgView.backgroundColor = .white
        bgView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 630*widthScale)
        addSubview(bgView)
        bgView.addCorner(conrners: [.topLeft,.topRight], radius: 18*widthScale)
        
        titleLabel = HWLabel.createBoldLabel(title: "游戏社区列表", color: "#333333", font: 16*widthScale)
        bgView.addSubview(titleLabel)
        
        closeButton = UIButton(type: .custom)
        closeButton.setTitle("关闭", for: .normal)
        closeButton.setTitleColor(.colorWithHexString(hexString: "#888888"), for: .normal)
        closeButton.titleLabel?.font = .systemFont(ofSize: 14*widthScale)
        closeButton.addTarget(self, action: #selector(closeButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(closeButton)
        
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = ["全部社区","我关注的社区"]
        
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.titleNormalColor = UIColor.colorWithHexString(hexString: "#333333")
        segmentedDataSource.titleSelectedColor = UIColor.colorWithHexString(hexString: "#FA8C08")
        segmentedDataSource.titleNormalFont = .systemFont(ofSize: 14*widthScale)
        segmentedDataSource.titleSelectedFont = .boldSystemFont(ofSize: 14*widthScale)
        segmentedDataSource.isItemSpacingAverageEnabled = false
        segmentedView.dataSource = segmentedDataSource
        
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorColor = .colorWithHexString(hexString: "#FA8C08")
        indicator.indicatorCornerRadius = 0
        indicator.verticalOffset = 0
        indicator.indicatorHeight = 4*widthScale
        segmentedView.indicators = [indicator]
        segmentedView.delegate = self
        segmentedView.backgroundColor = .clear
        bgView.addSubview(self.segmentedView)
        
        
        listContainerView = JXSegmentedListContainerView(dataSource: self)
        bgView.addSubview(listContainerView)
        
        segmentedView.listContainer = listContainerView
    }
    
    private func initFrame() -> Void {
        snp.makeConstraints { make in
            make.width.equalTo(screenWidth)
            make.height.equalTo(screenHeight)
        }
        
        tapBgView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(0)
        }
        
        bgView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(630*widthScale)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.top.equalTo(18*widthScale)
            make.height.equalTo(23*widthScale)
        }
        
        closeButton.snp.makeConstraints { make in
            make.right.equalTo(-18*widthScale)
            make.centerY.equalTo(titleLabel)
        }
        
        segmentedView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(18*widthScale)
            make.height.equalTo(30*widthScale)
        }
        listContainerView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(segmentedView.snp.bottom).offset(10*widthScale)
        }
        
    }
}
extension HWGameCommunityAllListSheet {
    @objc func bacgroundTap(_ tap:UITapGestureRecognizer) -> Void {
        hide()
    }
    
    @objc func closeButtonClick(_ button:UIButton) -> Void {
        hide()
    }
    
}


extension HWGameCommunityAllListSheet:JXSegmentedListContainerViewDataSource,JXSegmentedViewDelegate {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedDataSource.dataSource.count
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        let list = HWGameCommunityAllListController()
        list.didClickItem = {
            mod in
            self.hide()
        }
        list.isSheet = true
        if index == 0 {
            list.type = "1"
        }else {
            list.type = "2"
        }
        
        return list
        
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        
    }
}
